home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / a_grayscale.asm < prev    next >
Encoding:
Assembly Source File  |  2001-03-20  |  4.0 KB  |  167 lines

  1. ;    VirtualDub - Video processing and capture application
  2. ;    Copyright (C) 1998-2001 Avery Lee
  3. ;
  4. ;    This program is free software; you can redistribute it and/or modify
  5. ;    it under the terms of the GNU General Public License as published by
  6. ;    the Free Software Foundation; either version 2 of the License, or
  7. ;    (at your option) any later version.
  8. ;
  9. ;    This program is distributed in the hope that it will be useful,
  10. ;    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;    GNU General Public License for more details.
  13. ;
  14. ;    You should have received a copy of the GNU General Public License
  15. ;    along with this program; if not, write to the Free Software
  16. ;    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.     .386
  19.     .model    flat
  20.     .code
  21.  
  22.     public _asm_grayscale_run
  23.  
  24. ;asm_grayscale_run(
  25. ;    [esp+ 4] void *dst,
  26. ;    [esp+ 8] ulong width,
  27. ;    [esp+12] ulong height,
  28. ;    [esp+16] ulong stride);
  29. ;
  30. ; normal conversion equation is:
  31. ;    Y = 0.212671 * R + 0.715160 * G + 0.072169 * B;
  32. ;
  33. ; We use:
  34. ;    Y = (54 * R + 183 * G + 19 * B)/256;
  35. ;
  36. ; This set of instructions gives us 19:
  37. ;
  38. ;    lea    ebx,[eax*8+eax]
  39. ;    lea    eax,[ebx*2+eax]
  40. ;
  41. ; This set of instructions gives us 54:
  42. ;
  43. ;    add    ecx,ecx
  44. ;    lea    ecx,[ecx*2+ecx]
  45. ;    lea    ecx,[ecx*8+ecx]
  46. ;
  47. ; This set of instructions gives us 183: = 184-1 = 92*2-1 = 46*4-1 = 23*8-1 = (24-1)*8-1
  48. ;
  49. ;    lea    edx,[ebp*8+ebp]
  50. ;    lea    edx,[edx*4+edx]
  51. ;    lea    edx,[edx*2+ebp]
  52. ;    lea    edx,[edx*2+ebp]
  53.  
  54. _asm_grayscale_run:
  55.     push    ebp
  56.     push    edi
  57.     push    esi
  58.     push    edx
  59.     push    ecx
  60.     push    ebx
  61.     push    eax
  62.  
  63.     mov    esi,[esp+ 4+28]
  64.     mov    ebp,[esp+12+28]
  65.  
  66. grayscale@rowloop:
  67.     push    ebp
  68.     mov    ebp,[esp+ 8+32]
  69.  
  70.     ;the people in r.g.p are a little crazy... this code is based on that
  71.     ;which Robert Blum posted.
  72.     ;
  73.     ;me: 19 cycles (once the stupid loop-initial AGI is removed)
  74.     ;him: 15 cycles (doh!)
  75.  
  76.     IF 1
  77.     push    esi
  78. grayscale@colloop:
  79.     mov     eax,[esi]        ;1u EAX=?.R.G.B
  80.         xor     ebx,ebx            ;1v EBX=0
  81.         mov     edx,eax            ;2u EDX=?.R.G.B
  82.         and     eax,00ff00ffh        ;2v EAX=[R][B]
  83.         mov     bl,dh            ;3u EBX=[][G]
  84.         mov     edi,eax            ;3v ESI=[R][B]
  85.         xor     ecx,ecx            ;4u EDI=0
  86.         lea     edx,[eax+8*eax]        ;4v EDX=[R*9][B*9]
  87.         lea     ecx,[ebx+2*ebx]        ;5u ECX=[G*3]
  88.         lea     ebx,[ebx+8*ebx]        ;5v EBX=[G*9]
  89.         shl     ecx,6            ;6u ECX=[G*192]
  90.         lea     eax,[edx+2*edx]        ;6v EAX=[R*27][B*27]
  91.         lea     edi,[edi+2*edx]        ;7u ESI=[R*19][B*19]
  92.         sub     ecx,ebx            ;7v ECX=[183*G]
  93.         shr     eax,15            ;8u EAX=[54*R]
  94.         add     ecx,edi            ;8v ECX=[183*G+19*B]
  95.         add     eax,ecx            ;9u EAX=54*R+183*G+19*B
  96.     add    esi,4            ;9v
  97.     mov    ebx,eax            ;10u
  98.     mov    ecx,eax            ;10v
  99.         shr     eax,8            ;11u EAX=Y
  100.     and    ebx,0000ff00h        ;11v
  101.     shl    ecx,8            ;12u
  102.     and    eax,000000ffh        ;12v
  103.     or    eax,ebx            ;13u
  104.     and    ecx,00ff0000h        ;13v
  105.     or    eax,ecx            ;14u
  106.     dec    ebp            ;14v
  107.     mov    [esi-4],eax        ;15u
  108.     jne    grayscale@colloop    ;15v
  109.     pop    esi
  110.  
  111.     ELSE
  112.  
  113. grayscale@colloop:
  114.     mov    eax,[esi+ebp*4-4]    ;u
  115.     nop                ;v
  116.     mov    ecx,eax            ;u
  117.     mov    edx,eax            ;v
  118.     shr    ecx,16            ;u
  119.     and    eax,000000ffh        ;v EAX = blue
  120.     shr    edx,8            ;u
  121.     and    ecx,000000ffh        ;v ECX = red
  122.     lea    ebx,[eax*8+eax]        ;u **blue 1**
  123.     and    edx,000000ffh        ;v EDX = green
  124.     lea    ecx,[ecx*2+ecx]        ;u **red 1**
  125.     nop                ;v
  126.     lea    edi,[edx*2+edx]        ;u **green 1**
  127.     lea    eax,[ebx*2+eax]        ;v **blue 2** FINISHED
  128.     shl    edi,3            ;u **green 2**
  129.     add    ecx,ecx            ;v **red 2**
  130.     sub    edi,edx            ;u **green 3**
  131.     lea    ecx,[ecx*8+ecx]        ;v **red 3** FINISHED
  132.     shl    edi,3            ;u **green 4**
  133.     add    eax,ecx            ;v EAX = blue + red
  134.     sub    edi,edx            ;u
  135.     nop                ;v
  136.     add    eax,edi            ;u EAX = red + green + blue
  137.     nop                ;v
  138.     mov    ebx,eax            ;u
  139.     mov    ecx,eax            ;v
  140.     shl    ecx,8            ;u
  141.     and    ebx,0000ff00h        ;v
  142.     shr    eax,8            ;u
  143.     and    ecx,00ff0000h        ;v
  144.     or    eax,ebx            ;u
  145.     or    eax,ecx            ;u
  146.     mov    [esi+ebp*4-4],eax    ;u
  147.     dec    ebp            ;u
  148.     jne    grayscale@colloop    ;v
  149.     ENDIF
  150.  
  151.     pop    ebp
  152.     add    esi,[esp+16+28]
  153.  
  154.     dec    ebp
  155.     jne    grayscale@rowloop
  156.  
  157.     pop    eax
  158.     pop    ebx
  159.     pop    ecx
  160.     pop    edx
  161.     pop    esi
  162.     pop    edi
  163.     pop    ebp
  164.     ret
  165.  
  166.     end
  167.